//
//  FlutterNativeWXSharePlugin.m
//  Runner
//
//  Created by 小二同學 on 2020/10/30.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

#import "FlutterNativeWXSharePlugin.h"
#import "WXApi.h"
#import <FinApplet/FATClient.h>
#import <FinAppletWXExt/FinAppletWXExt.h>

@interface FlutterNativeWXSharePlugin()

@property (nonatomic, strong) UIViewController *controller;
@end

@implementation FlutterNativeWXSharePlugin

static NSString *const CHANNELNAME = @"com.ahj/wxSharePlugin";
static NSString *const METHODNAME  = @"wxShareProgram";
static NSString *const SHAREURLNAME  = @"wxShareUrl";
static NSString *const SHAREPICNAME  = @"wxSharePic";
static NSString *const SHAREFILENAME  = @"wxShareFile";
static NSString *const SCMETHODNAME  = @"wxShareSCProgram";
static NSString *const WXID = @"wx83cf55a5a3baaba0";
static NSString *const UniversalLink = @"https://com.anHuanJia.xinanli/";

/**
 *plugin 注册
 */
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {

    //注册微信小程序
    [WXApi registerApp:WXID universalLink:UniversalLink];

//    //调用自检函数
//    [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
//        NSLog(@"%@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion);
//    }];
    
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:CHANNELNAME binaryMessenger:[registrar messenger]];
    
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    FlutterNativeWXSharePlugin *plugin = [[FlutterNativeWXSharePlugin alloc]initWithViewController:viewController];
    
    [registrar addMethodCallDelegate:plugin channel:channel];
}

/**
 *flutter端通过通道调用原生方法时会进入以下回调
 */
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
   //call的属性method是flutter调用原生方法的方法名，我们进行字符串判断然后写入不同的逻辑
            NSString *methodName = call.method;

            if ([methodName isEqualToString:METHODNAME]) {
           
                if (![WXApi isWXAppInstalled]) {
                    // result(@"您没有安装微信");
                    // [self kAlertAction:@"您没有安装微信"];
                    return;
                }
                
                [self wxShareWithInfo:call.arguments];
            }else if ([methodName isEqualToString:SCMETHODNAME]) {
                if (![WXApi isWXAppInstalled]) {
                    return;
                }
                
                [self wxSCShareWithInfo1:call.arguments];
            }else if ([methodName isEqualToString:SHAREURLNAME]) {
                  if (![WXApi isWXAppInstalled]) {
                            return;
                  }
              [self wxShareUrlWithInfo:call.arguments];
             }else if ([methodName isEqualToString:SHAREPICNAME]) {
                if (![WXApi isWXAppInstalled]) {
                    return;
                }
                
                [self wxSCShareWithPic:call.arguments];
            }else if ([methodName isEqualToString:SHAREFILENAME]) {
                if (![WXApi isWXAppInstalled]) {
                    return;
                }
                
                [self wxSCShareWithFile:call.arguments];
            }
            else{
                //调用的方法原生没有对应的处理  抛出未实现的异常
                result(FlutterMethodNotImplemented);
            }
}

- (void)wxShareUrlWithInfo:(NSDictionary *)dic {
//    NSString *title = [NSString stringWithFormat:@"%@",dic[@"title"]];
//    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
//    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
//    NSString *shareType = [NSString stringWithFormat:@"%@",dic[@"shareType"]];
//
//
//    if ([url hasPrefix:@"http"]) {
//        WXWebpageObject *webpageObject = [WXWebpageObject object];
//        webpageObject.webpageUrl = dic[@"url"];
//
//        WXMediaMessage *message = [WXMediaMessage message];
//        if (kStringIsEmpty(goodsName)) {
//            message.title = @"青柠";
//        }else {
//            message.title = goodsName;
//        }
//        message.description = description1;
//
//
//        message.mediaObject = webpageObject;
//        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//        req.bText = NO;
//        req.message = message;
//        req.scene = shareType == "01" ? WXSceneSession:WXSceneTimeline;
//
//        weixin://wap/pay?prepayid%3Dwx06094454714147e5025508723b6e340000&package=4218714087&noncestr=1604627101&sign=43a491eaf14d351ee93144b7938d8955&redirect_url=uu.qunayue.com
//
//    //    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//    //    req.bText = YES;
//    //    req.text = dic[@"url"];
//    //    req.scene = WXSceneSession;
//        [WXApi sendReq:req completion:^(BOOL success) {
//
//        }];
//    }else {
//
//        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//        req.bText = YES;
//        req.text = dic[@"url"];
//        req.scene = WXSceneSession;
//        [WXApi sendReq:req completion:^(BOOL success) {
//
//        }];
//    }
}



- (void)wxShareWithInfo:(NSDictionary *)dic {
    NSString *goodsName = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
    
    if ([url hasPrefix:@"http"]) {
        WXWebpageObject *webpageObject = [WXWebpageObject object];
        webpageObject.webpageUrl = dic[@"url"];
        WXMediaMessage *message = [WXMediaMessage message];
        if (kStringIsEmpty(goodsName)) {
            message.title = @"青柠";
        }else {
            message.title = goodsName;
        }
        message.description = kStringIsEmpty(description1) ? @"免费提供企业账号注册" : description1;
        
        if (kStringIsEmpty(imageUrl)) {
            imageUrl = @"";
        }
        NSData *compressData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        UIImage *image = [UIImage imageWithData:compressData];
        if (kStringIsEmpty(imageUrl)) {
            image = [UIImage imageNamed:@"ahj_logo"];
        }
        compressData = UIImageJPEGRepresentation(image, 0.1);
        [message setThumbImage:[UIImage imageWithData:compressData]];
                                
        message.mediaObject = webpageObject;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneSession;
        
        weixin://wap/pay?prepayid%3Dwx06094454714147e5025508723b6e340000&package=4218714087&noncestr=1604627101&sign=43a491eaf14d351ee93144b7938d8955&redirect_url=uu.qunayue.com
        
    //    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    //    req.bText = YES;
    //    req.text = dic[@"url"];
    //    req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }else {

        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = YES;
        req.text = dic[@"url"];
        req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }
}

- (void)wxSCShareWithInfo:(NSDictionary *)dic {
    NSString *goodsName = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
    
    if ([url hasPrefix:@"http"]) {
        WXWebpageObject *webpageObject = [WXWebpageObject object];
        webpageObject.webpageUrl = dic[@"url"];
        WXMediaMessage *message = [WXMediaMessage message];
        if (kStringIsEmpty(goodsName)) {
            message.title = @"青柠";
        }else {
            message.title = goodsName;
        }
        message.description = kStringIsEmpty(description1) ? @"免费提供企业账号注册" : description1;
//
//        UIImage *image = [UIImage imageWithData:compressData];
//        if (kStringIsEmpty(imageUrl)) {
//            image = [UIImage imageNamed:@"ahj_logo"];
//        }
//        compressData = UIImageJPEGRepresentation(image, 0.1);
//        [message setThumbImage:[UIImage imageWithData:compressData]];
                                
        message.mediaObject = webpageObject;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneSession;
        
        weixin://wap/pay?prepayid%3Dwx06094454714147e5025508723b6e340000&package=4218714087&noncestr=1604627101&sign=43a491eaf14d351ee93144b7938d8955&redirect_url=uu.qunayue.com
        
    //    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    //    req.bText = YES;
    //    req.text = dic[@"url"];
    //    req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }else {

        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = YES;
        req.text = dic[@"url"];
        req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }
}

- (void)wxSCShareWithPic:(NSDictionary *)dic {
    NSString *goodsName = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *imageFileUrl = [NSString stringWithFormat:@"%@",dic[@"fileUrl"]];
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *sharType = [NSString stringWithFormat:@"%@",dic[@"sharType"]];
    
    if ([url hasPrefix:@"http"]) {
        WXWebpageObject *webpageObject = [WXWebpageObject object];
        webpageObject.webpageUrl = dic[@"url"];
        WXMediaMessage *message = [WXMediaMessage message];
//        if (kStringIsEmpty(goodsName)) {
//            message.title = @"青柠";
//        }else {
//            message.title = goodsName;
//        }
//        message.description = kStringIsEmpty(description1) ? @"免费提供企业账号注册" : description1;
        if (kStringIsEmpty(imageUrl)) {
            imageUrl = @"";
        }
        
        if([imageUrl hasPrefix:@"finfile:"]){
            imageUrl = [[FATClient sharedClient] fat_absolutePathWithPath:imageUrl];
        }
        
        NSData *compressData;
        if (!kStringIsEmpty(imageFileUrl)) {
            compressData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imageFileUrl]];
        }else {
            compressData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        }
        
        UIImage *image = [UIImage imageWithData:compressData];
        if (kStringIsEmpty(imageUrl)) {
            image = [UIImage imageNamed:@"ahj_logo"];
        }
        compressData = UIImageJPEGRepresentation(image, 0.1);
        [message setThumbImage:[UIImage imageWithData:compressData]];
        
        WXImageObject *ext = [WXImageObject object];
                // 小于10MB
        ext.imageData = compressData;
                                
        message.mediaObject = ext;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        
        if ([sharType isEqual:@"0"]) {
            req.scene = WXSceneSession;
        }else {
            req.scene = WXSceneTimeline;
        }
        
        weixin://wap/pay?prepayid%3Dwx06094454714147e5025508723b6e340000&package=4218714087&noncestr=1604627101&sign=43a491eaf14d351ee93144b7938d8955&redirect_url=uu.qunayue.com
        
    //    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    //    req.bText = YES;
    //    req.text = dic[@"url"];
    //    req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }else {

        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = YES;
        req.text = dic[@"url"];
        req.scene = WXSceneSession;
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
    }
}

- (void)wxSCShareWithFile:(NSDictionary *)dic {
    NSString *goodsName = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *sharType = [NSString stringWithFormat:@"%@",dic[@"sharType"]];
    
    WXMediaMessage *message = [WXMediaMessage message];
    [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString * _Nonnull log) {
        NSLog(@"%@",log);
    }];
    if (kStringIsEmpty(imageUrl)) {
        imageUrl = @"";
    }
    
    NSData *compressData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imageUrl]];
    NSString *textFile  = [[NSString alloc] initWithData:compressData encoding:NSUTF8StringEncoding];
    
    WXFileObject *ext = [WXFileObject object];
            // 小于10MB
    ext.fileData = compressData;
    ext.fileExtension = textFile;
    
    NSData *compressData1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        UIImage *image = [UIImage imageWithData:compressData];
        if (kStringIsEmpty(imageUrl)) {
            image = [UIImage imageNamed:@"ahj_logo"];
        }
    compressData1 = UIImageJPEGRepresentation(image, 0.1);
        [message setThumbImage:[UIImage imageWithData:compressData1]];
                            
    message.mediaObject = ext;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    message.title = goodsName;
    req.message = message;
    
    req.scene = WXSceneSession;
    
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
    
}

- (void)wxSCShareWithInfo1:(NSDictionary *)dic {
    NSString *goodsName = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
    NSString *description1 = [NSString stringWithFormat:@"%@",dic[@"description"]];
    NSString *imageUrl = [NSString stringWithFormat:@"%@",dic[@"imageUrl"]];
    NSString *url = [NSString stringWithFormat:@"%@",dic[@"url"]];
    NSString *sharType = [NSString stringWithFormat:@"%@",dic[@"sharType"]];
    
    WXWebpageObject *webpageObject = [WXWebpageObject object];
    webpageObject.webpageUrl = dic[@"url"];
    WXMediaMessage *message = [WXMediaMessage message];
    if (kStringIsEmpty(goodsName)) {
        message.title = @"青柠";
    }else {
        message.title = goodsName;
    }
    message.description = kStringIsEmpty(description1) ? @"免费提供企业账号注册" : description1;
//
    if (kStringIsEmpty(imageUrl)) {
        imageUrl = @"";
    }
    NSData *compressData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
        UIImage *image = [UIImage imageWithData:compressData];
        if (kStringIsEmpty(imageUrl)) {
            image = [UIImage imageNamed:@"ahj_logo"];
        }
        compressData = UIImageJPEGRepresentation(image, 0.1);
        [message setThumbImage:[UIImage imageWithData:compressData]];
                            
    message.mediaObject = webpageObject;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    if ([sharType isEqual:@"0"]) {
        req.scene = WXSceneSession;
    }else {
        req.scene = WXSceneTimeline;
    }
    
    weixin://wap/pay?prepayid%3Dwx06094454714147e5025508723b6e340000&package=4218714087&noncestr=1604627101&sign=43a491eaf14d351ee93144b7938d8955&redirect_url=uu.qunayue.com
    
//    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//    req.bText = YES;
//    req.text = dic[@"url"];
//    req.scene = WXSceneSession;
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}
@end
